﻿//
// ToDoList v2.0
// Onat Yigit Mercan
// onatmercan [at] gmail [dot] com
//
using System;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.IO;
using System.Windows.Interop;
using System.Timers;

namespace ToDoList
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private IntPtr Handle { get { return new WindowInteropHelper(this).Handle; } }
        private FileHandler fileHandler;
        private bool pinned = false;
        private string directoryPath;
        private string fileName = "todo.txt";

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            if (!pinned)
                this.DragMove();
        }

        public MainWindow()
        {
            InitializeComponent(); 
        }

        #region event handlers
        //
        // Event handlers
        //
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (Properties.Settings.Default.InFront)
                Win32.SetWindowInFront(Handle);
            else
                Win32.SetWindowInBack(Handle);

            if (Properties.Settings.Default.IsCustomPath)
                this.directoryPath = Properties.Settings.Default.directoryPath;
            else
                this.directoryPath = Environment.GetFolderPath(
                    Environment.SpecialFolder.MyDocuments);

            fileHandler = new FileHandler(this.directoryPath, this.fileName);
            txtToDo.Text = fileHandler.ReadFile();
        }
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            fileHandler.WriteFile(txtToDo.Text);
        }
        private void miPin_Click(object sender, RoutedEventArgs e)
        {
            fileHandler.WriteFile(txtToDo.Text);
            pinned = true;
            txtToDo.IsEnabled = false;
        }
        private void miUnpin_Click(object sender, RoutedEventArgs e)
        {
            pinned = false;
            txtToDo.IsEnabled = true;
        }
        private void miConfiguration_Click(object sender, RoutedEventArgs e)
        {
            ConfigurationWindow configurationWindow = new ConfigurationWindow(Handle, fileHandler);
            configurationWindow.Show();
            configurationWindow.Closing += new System.ComponentModel.CancelEventHandler(configurationWindow_Closing);
        }
        private void miExit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        private void configurationWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            txtToDo.Text = fileHandler.ReadFile();
        }
        #endregion
    }
}
